#!/usr/bin/env python
from jscrunch import *
import unittest
class JsCrunchTest(unittest.TestCase):
    """"""
    def setUp(self):
        self.lStr =r"""if(dummy){t += "_'// \"" + literalStrings.length + "_\'_";}else{}//ab"""
        self.reslStr=[(False,'if(dummy){t += '),(True,r'''"_'// \""'''),
                      (False,' + literalStrings.length + '),
                      (True,r'''"_\'_"'''),
                      (False,';}else{}')]
        self.rxStr =r"""var r =/\s+\/"[^x]+$/g;blah = s.search(r);"""
        self.resxrStr = [(False,"var r ="),(True,r"""/\s+\/"[^x]+$/"""),
                         (False,"g;blah = s.search(r);")]
        self.spaced = """if ( x ) { [ 0 , '' ] ;} """
        self.despaced = """if(x){[0,''];}"""
        self.resjs = r"""function removeComments(s){var lines,i,t;lines=s.split("\n");t="";for(i=0;i<lines.length;i++)t+=lines[i].replace(/([^\x2f]*)\x2f\x2f.*$/,"$1");t=t.replace(/(.*)\n(.*)/g,"$1 $2");lines=t.split("*/");t="";for(i=0;i<lines.length;i++)t+=lines[i].replace(/(.*)\x2f\x2a(.*)$/g,"$1 ");return t;}"""
        
    def test_line_with_str(self):
        self.assertEqual(self.reslStr,tokenizeLitStr(self.lStr))
    def test_line_with_re(self):
        self.assertEqual(self.resxrStr,tokenizeLitStr(self.rxStr))
##    def test_despace(self):
##        self.assertEqual(self.despaced,deSpace(self.spaced))
    def test_jssnippet(self):
        with open('jscrunch_in.txt') as fin:
            with open('jscrunch_out.txt','w') as fout:
                sys.stdin = fin
                sys.stdout = fout
                crunch()
        with open('jscrunch_out.txt') as fout:
            s = fout.read()
            self.assertEqual(self.resjs,s)
        
        
        
        
if __name__ == '__main__':
    unittest.main()
